﻿# 获取当前目录
$currentDir = Split-Path -Parent $MyInvocation.MyCommand.Path

# 定义 XML 文件路径
$xmlFilePath = Join-Path -Path $currentDir -ChildPath "installToLocal.xml"

# 读取 XML 文件
[xml]$xml = Get-Content -Path $xmlFilePath

# 定义输出文件路径
$outputFilePath = Join-Path -Path $currentDir -ChildPath "mvn_commands.txt"

# 清空或创建输出文件
New-Item -Path $outputFilePath -ItemType File -Force

# 定义 source 子目录路径
$sourceDir = Join-Path -Path $currentDir -ChildPath "source"

# 遍历每个 dependency
foreach ($dependency in $xml.project.dependencies.dependency) {
    $groupId = $dependency.groupId
    $artifactId = $dependency.artifactId
    $version = $dependency.version

    # 构造 Maven 命令的 JAR 文件路径
    $jarFilePath = Join-Path -Path $sourceDir -ChildPath "$artifactId-$version.jar"
    $pomFilePath = Join-Path -Path $sourceDir -ChildPath "$artifactId-$version.pom"

    # 执行 JAR 文件安装命令
    if (Test-Path $jarFilePath) {
        $jarCommand = "mvn install:install-file -Dfile=`"$jarFilePath`" -DgroupId=$groupId -DartifactId=$artifactId -Dversion=$version -Dpackaging=jar"
        Add-Content -Path $outputFilePath -Value $jarCommand
    }

    # 执行 POM 文件安装命令
    if (Test-Path $pomFilePath) {
        $pomCommand = "mvn install:install-file -Dfile=`"$pomFilePath`" -DgroupId=$groupId -DartifactId=$artifactId -Dversion=$version -Dpackaging=pom"
        Add-Content -Path $outputFilePath -Value $pomCommand
    }
}

Write-Output "Maven commands have been saved to $outputFilePath"